package com.yueya.basic.user.model;

import java.util.List;

import com.jfinal.aop.Duang;
import com.jfinal.kit.StrKit;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Model;
import com.jfinal.plugin.activerecord.Record;
import com.yueya.basic.user.service.UserService;

/**
 * User model.
 */
@SuppressWarnings("serial")
public class User extends Model<User> {
	public static final User dao = new User();

	public static UserService service = Duang.duang(UserService.class);

	public List<User> findAll() {
		return find("SELECT * FROM t_user");
	}

	// 所有未被删除的用户
	public List<User> findNotDel() {
		return find("SELECT * FROM t_user WHERE DEL_FLAG='0'");
	}

	public Record getUserByLoginName(String loginName) {
		Record record = Db.findFirst("select * from t_user i where i.login_name ='" + loginName + "'");
		return record;
	}

	public static String genRandom() {
		String n[] = { "a", "b", "c", "d", "e", "f", "h", "j", "k", "m", "n", "p", "r", "s", "t", "u", "v", "w", "x",
				"y", "z", "2", "3", "4", "5", "6", "7", "8", "9" };
		String str = "";
		int index = 0;
		for (int i = 0; i < 6; i++) {
			index = 1 + (int) (Math.random() * 28);

			str += n[index];

		}
		return str;
	}

	public Record findUserDetailById(String userId) {
		String sql = "select * from t_user u inner join t_user_ext ue on  u.id = ue.user_id where u.id = ?";
		return Db.findFirst(sql , userId);
		
	}
	
	public List<User> findLetterList(String username){
		String sql="select u.user_name username, u.login_name tel ,left(u.user_pinyin,1) letter ,b.position  position ,b.avatar_path  avatarpath";
				sql+= " from t_user u ,t_user_base  b where u.del_flag = '0' and u.user_id =b.user_id and u.user_id <> '0001'";
				if(StrKit.notBlank(username)) {
					sql+="  and u.user_name like '%"+username+"%'";						
				}
				sql+=  " order by  u.user_pinyin asc ";
	    return User.dao.find(sql);
	}
}